/*********************************************************
*
*  Project:      Utilities
*
*  Class:        PropertyHandler
*
*  File name:    PropertyHandler.java
*
*
*    SVN:
*
*    $Rev$:
*    $Date$:
*    $LastChangedBy$:
*
*    $HeadURL$:
*
**********************************************************/

package com.eastmountain.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.eastmountain.util.logging.LoggerUtils;

/*********************************************************
 *
 *  Class:   PropertyHandler
 *
 **********************************************************/

public class PropertyHandler {

    private static Properties defaultProps = null;
    private static Properties applicationProps = null;
    
    private static String PROPERTIES_FILE_NAME = "HZFlashCards.properties";
    
    private static FileInputStream fileInputStream = null;

    private static String projectName = "hzflashcards";
    private static Logger logger = LoggerUtils.setUpLogger( projectName );

    // create and load default properties

    public static Properties load() {

        defaultProps = null;
        applicationProps = null;

        fileInputStream = null;

        try {

            fileInputStream = new FileInputStream( "defaultProperties" );
            defaultProps.load( fileInputStream );
            fileInputStream.close();

        } catch ( FileNotFoundException exception ) {

            logger.log( Level.INFO, "Default properties not found" );

        } catch ( IOException exception ) {

            logger.log( Level.INFO, "Default properties not found" );

        }

        try {

            if ( defaultProps != null ) {

                applicationProps = new Properties( defaultProps );

            } else {

                applicationProps = new Properties();

            }

            fileInputStream = new FileInputStream( PROPERTIES_FILE_NAME );
            applicationProps.load( fileInputStream );
            fileInputStream.close();

        } catch ( FileNotFoundException fileNotFoundException ) {

            logger.log( Level.WARNING, "Properties not found" );
            System.out.println( "Property file is missing" );

        } catch ( IOException exception ) {

            logger.log( Level.WARNING, "Properties not found" );
            System.out.println( "Property file is missing" );

        }

        return( applicationProps );

    }

    public static void store( Properties applicationProps ) {

        try {

            FileOutputStream fileOutputStream = new FileOutputStream( PROPERTIES_FILE_NAME );
            applicationProps.store( fileOutputStream, "--- HZFlashCards.properties ---" );
            fileOutputStream.close();

        } catch ( FileNotFoundException exception ) {

            // TODO Auto-generated catch block
            exception.printStackTrace();

        } catch ( IOException exception ) {

            // TODO Auto-generated catch block
            exception.printStackTrace();

        }

    }

    public static void main( String[] args ) {

        Properties applicationProps = new Properties();

        applicationProps.setProperty( "lessons", "1, 2, 4, 7, 9" );

        PropertyHandler.store( applicationProps );

    }

}
